package it.apiconsulting.wot.controller;


import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;

import it.apiconsulting.wot.model.SchedaFood;
import it.apiconsulting.wot.util.HibernateUtil;


public class SchedaFoodManager extends HibernateUtil {
	
	public SchedaFood add(SchedaFood schedaFood) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(schedaFood);
		session.getTransaction().commit();
		return schedaFood;
	}
	public SchedaFood delete(Long id) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		SchedaFood schedaFood = (SchedaFood) session.load(SchedaFood.class, id);
		if(null != schedaFood) {
			session.delete(schedaFood);
		}
		session.getTransaction().commit();
		return schedaFood;
	}

	public List<SchedaFood> list() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<SchedaFood> schedeFood = null;
		try {
			
			Query query = session.createQuery("from SchedaFood");
			schedeFood = (List<SchedaFood>)query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return schedeFood;
	}
	
	public List<SchedaFood> listByIdUtente(Long idUtente) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<SchedaFood> schedeFood = null;
		try {
			
			Query query = session.createQuery("FROM SchedaFood WHERE id_utente = "+idUtente);
			schedeFood = (List<SchedaFood>)query.list();
			
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return schedeFood;
	}
	
	public List<SchedaFood> listByIdScheda(Long idScheda) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<SchedaFood> schedeFood = null;
		try {
			
			Query query = session.createQuery("FROM SchedaFood WHERE id_scheda = "+idScheda);
			schedeFood = (List<SchedaFood>)query.list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return schedeFood;
	}
	
	public List<SchedaFood> listByIdUFood(Long idFood) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<SchedaFood> schedeFood = null;
		try {
			
			Query query = session.createQuery("FROM SchedaFood WHERE id_food = "+idFood);
			schedeFood = (List<SchedaFood>)query.list();
			
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return schedeFood;
	}
	
	public List<Long> listSchedeByIdUtente(Long idUtente, Integer numSchede) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Long> schede = new ArrayList<Long>();
		try {
			
			Query query = session.createQuery("FROM SchedaFood WHERE id_utente = " + idUtente + " GROUP BY id_scheda ORDER BY scheda_date desc");
			List<SchedaFood> schedeFood = (List<SchedaFood>)query.list();
			if (numSchede!=null) { 
				for (int i = 0; (i<numSchede && i<schedeFood.size()); i++) {
					SchedaFood schedaFood = schedeFood.get(i);
					schede.add(schedaFood.getIdScheda());
				}
			} else { 
				for ( SchedaFood schedaFood : schedeFood ) {
					schede.add(schedaFood.getIdScheda());
				}
			}
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return schede;
	}
}
